<template>
  <el-dialog
    class="elDialog"
    @close="closeDialog"
    :title="this.addDirectory.id?'编辑目录':'新增目录' "
    :visible="addDialogVisible"
    width="30%"
    @open="openDialog"
  >
    <el-form
      ref="addDirectory"
      label-width="80px"
      :model="addDirectory"
      :rules="rules"
    >
      <el-form-item v-if="isTrue" label="所属学科" prop="subjectID">
        <el-select v-model="addDirectory.subjectID" placeholder="请选择">
          <el-option
            v-for="item in catalogList"
            :key="item.subjectName"
            :label="item.subjectName"
            :value="item.subjectID">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="目录名称" prop="directoryName">
        <el-input v-model="addDirectory.directoryName" ></el-input>
      </el-form-item>
    </el-form>
    <div class="btnRight">
    <span slot="footer" >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="confirm">确 定</el-button>
    </span>
    </div>
  </el-dialog>
</template>

<script>
import { add, update, list } from '@/api/hmmm/directorys.js'

export default {
  name: 'directorys-add',
  props: {
    addDialogVisible: {
      type: Boolean
    }
  },
  data () {
    return {
      catalogList: [],
      addDirectory: {
        directoryName: '',
        subjectID: '',
        id: null

      },
      isTrue: true,
      rules: {
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' }
        ],
        subjectID: [
          { required: true, message: '请选择所属学科', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async openDialog () {
      const res = await list({ page: 1, pagesize: 1000 })
      const map = {}
      res.data.items.forEach(item => {
        map[item.subjectName] = item
      })
      this.catalogList = Object.values(map)
      if (this.$route.query.row) {
        this.addDirectory.subjectID = this.$route.query.row.id
      }
      this.isTrue = !this.$route.query.row
    },
    resetForm () {
      this.$refs.addDirectory.resetFields()
      this.addDirectory = {
        directoryName: '',
        subjectID: '',
        id: null

      }
    },
    closeDialog () {
      this.resetForm()
      this.$emit('closeDialog')
    },
    processDate (value) {
      const map = {
        false: 0,
        true: 1
      }
      return map[value]
    },
    getDate (value) {
      const map = {
        0: false,
        1: true
      }
      return map[value]
    },
    confirm () {
      this.$refs.addDirectory.validate(async (valid) => {
        if (!valid) return
        if (this.addDirectory.id) {
          await update(this.addDirectory)
        } else {
          await add(this.addDirectory)
        }
        this.$message.success(`${this.addDirectory.id ? '编辑' : '新增'}成功`)
        this.closeDialog()
      })
    },
    // 编辑
    editDirectory (id) {
      this.addDirectory = id
    }
  }

}
</script>
<style lang='scss' scoped>
.btnRight{
  text-align: right;
}
</style>
